// SPDX-License-Identifier: GPL-2.0
/*
 * MAX6675 Temperature Sensor Test Program
 *
 * 这个测试程序用于验证MAX6675驱动的功能：
 * 1. sysfs接口的可访问性
 * 2. 温度读取功能
 * 3. 温度单位切换功能
 * 4. 采样频率设置功能
 * 
 * 使用方法：
 * 1. 编译: gcc -o max6675_test max6675_test.c
 * 2. 运行: ./max6675_test
 * 
 * 注意事项：
 * - 运行前确保MAX6675驱动已正确加载
 * - 确保有正确的sysfs访问权限
 * - 热电偶需要正确连接
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>

/* sysfs路径定义 */
#define TEMP_RAW_PATH "/sys/bus/iio/devices/iio:device0/in_temp_raw"
#define TEMP_UNIT_PATH "/sys/bus/iio/devices/iio:device0/temperature_unit"
#define SAMPLING_FREQ_PATH "/sys/bus/iio/devices/iio:device0/sampling_frequency"

/**
 * read_sysfs_file - 读取sysfs文件内容
 * @path: 文件路径
 * @buf: 输出缓冲区
 * @size: 缓冲区大小
 *
 * 返回值：
 *   0: 成功
 *  -1: 失败
 */
static int read_sysfs_file(const char *path, char *buf, size_t size)
{
    int fd, ret;

    fd = open(path, O_RDONLY);
    if (fd < 0) {
        printf("Error opening %s: %s\n", path, strerror(errno));
        return -1;
    }

    ret = read(fd, buf, size - 1);
    close(fd);

    if (ret < 0) {
        printf("Error reading %s: %s\n", path, strerror(errno));
        return -1;
    }

    buf[ret] = '\0';
    return 0;
}

/**
 * write_sysfs_file - 写入sysfs文件内容
 * @path: 文件路径
 * @buf: 要写入的数据
 *
 * 返回值：
 *   0: 成功
 *  -1: 失败
 */
static int write_sysfs_file(const char *path, const char *buf)
{
    int fd, ret;

    fd = open(path, O_WRONLY);
    if (fd < 0) {
        printf("Error opening %s for writing: %s\n", path, strerror(errno));
        return -1;
    }

    ret = write(fd, buf, strlen(buf));
    close(fd);

    if (ret < 0) {
        printf("Error writing to %s: %s\n", path, strerror(errno));
        return -1;
    }

    return 0;
}

/**
 * read_temperature - 读取并转换温度值
 *
 * 读取原始温度值并转换为实际温度(°C)
 * 转换公式：实际温度 = 原始值 * 0.25
 *
 * 返回值：
 *   >= 0: 温度值(°C)
 *   -1: 读取失败
 */
static float read_temperature(void)
{
    char buf[32];
    int raw_temp;
    float temp;

    if (read_sysfs_file(TEMP_RAW_PATH, buf, sizeof(buf)) < 0)
        return -1;

    raw_temp = atoi(buf);
    temp = raw_temp * 0.25;  // 转换为实际温度值

    return temp;
}

/**
 * main - 主测试流程
 *
 * 测试步骤：
 * 1. 读取当前温度单位
 * 2. 切换到华氏度
 * 3. 设置采样频率
 * 4. 连续读取温度10次
 * 5. 切回摄氏度
 */
int main(int argc, char *argv[])
{
    char buf[32];
    float temp;
    int i;

    printf("MAX6675 Temperature Sensor Test Program\n");
    printf("======================================\n\n");

    // 测试1: 读取当前温度单位
    printf("Test 1: Reading temperature unit\n");
    printf("--------------------------------\n");
    if (read_sysfs_file(TEMP_UNIT_PATH, buf, sizeof(buf)) == 0)
        printf("Current temperature unit: %s", buf);

    // 测试2: 切换温度单位
    printf("\nTest 2: Switching temperature unit\n");
    printf("--------------------------------\n");
    printf("Switching to Fahrenheit...\n");
    if (write_sysfs_file(TEMP_UNIT_PATH, "fahrenheit") == 0)
        printf("Successfully switched to Fahrenheit\n");

    // 测试3: 设置采样频率
    printf("\nTest 3: Setting sampling frequency\n");
    printf("--------------------------------\n");
    printf("Setting sampling frequency to 1000ms...\n");
    if (write_sysfs_file(SAMPLING_FREQ_PATH, "1000") == 0)
        printf("Successfully set sampling frequency\n");

    // 测试4: 连续读取10次温度
    printf("\nTest 4: Reading temperature 10 times\n");
    printf("--------------------------------\n");
    for (i = 0; i < 10; i++) {
        temp = read_temperature();
        if (temp >= 0) {
            printf("Temperature reading %d: %.2f°C\n", i + 1, temp);
        } else {
            printf("Failed to read temperature\n");
        }
        sleep(1);  // 等待1秒
    }

    // 测试5: 切回摄氏度
    printf("\nTest 5: Switching back to Celsius\n");
    printf("--------------------------------\n");
    if (write_sysfs_file(TEMP_UNIT_PATH, "celsius") == 0)
        printf("Successfully switched back to Celsius\n");

    printf("\nTest completed.\n");
    return 0;
} 